package com.linkstec.bookshop.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.linkstec.bookshop.pojo.User;
import com.linkstec.bookshop.service.UserService;

/**
 * 
 * @ClassName:  RegistController  用户注册 
 * @Description:TODO   
 * @author:  JGQiang
 * @date:   2018年11月27日 下午4:04:36
 */
@Controller
@RequestMapping("regist")
public class RegistController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 
	 * @Description:TODO 跳转到注册页
	 * @author: JGQiang
	 * @date:   2018年11月27日 下午5:15:14    
	 * @return
	 */
	@RequestMapping(value = "toRegist")
	public String toRegist() {
		return "regist";
	}
	
	@ResponseBody
	@RequestMapping(value="registFindUserByName")
	public String registFindUserByName(String uname){
		JSONObject json = new JSONObject();
		User user =userService.findUserByName(uname);
		if (user==null) {
			json.put("msg", "用户名可用");
		}else {
			json.put("msg", "用户名不可用");
		}
		
		return json.toString();
	}
	
	
	/**
	 * 
	 * @Description:TODO 注册
	 * @author: JGQiang
	 * @date:   2018年11月27日 下午5:15:02    
	 * @param user
	 * @param req
	 * @param checkImg
	 * @return
	 */
	@RequestMapping(value = "regist")
	public String regist(User user,HttpServletRequest req, String checkImg) {
		String sessionCode = (String) req.getSession().getAttribute("checkcode");
		if (!sessionCode.equalsIgnoreCase(checkImg)) {
			req.getSession().setAttribute("message", "验证码输入错误请重新注册");
			return "redirect:/regist/toRegist";
		}else{
		try {
			userService.registerUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		req.getSession().removeAttribute("message");
		return "redirect:/login/toLogin";
		}
	}

}
